package com.linkage.bss.crm.packagemgr.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;


/**
 *  文件管理类
 * @ClassName: FileManager.java
 * @Description: FileManager.java
 * @author: JinShuming
 * @date 2012-10-8
 */
public class FileManager extends DataGridManager{

	private static final long serialVersionUID = 1L;

	public String getImageFiles(){
		
		String filePath = "";

		File[] images = null;
		
		try{
			
			
			HttpServletRequest request = ServletActionContext.getRequest();
			filePath = request.getRealPath("/");  
			
			File file = new File(filePath + "/resources/images/package");
			
			images = file.listFiles();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		List<ImageFile> imageFiles = new ArrayList<ImageFile>();
		
		if(images != null){
			for(File f : images){
				ImageFile imageFile = new ImageFile();
				imageFile.setPath(f.getPath().replace(filePath, "").replace("\\", "/"));
				imageFiles.add(imageFile);
			}
			Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
			//json转化属性过滤，忽略掉值为null的属性
			jsonMap.put("total", imageFiles.size());
			jsonMap.put("rows", imageFiles);//rows键 存放每页记录 list  
			try{
				result = JSONObject.fromObject(jsonMap, jsonConfig);//格式化result   一定要是JSONObject
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * 内部类
	 * @version  [版本号, 2013-4-28]
	 * @see  [相关类/方法]
	 * @since  [产品/模块版本]
	 */
	public class ImageFile{
		String path = "";

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}
	}
}
